<?php

namespace app\distribution\controller\sys_admin;

use app\AdminController;
use app\distribution\model\RegionRewardModel;



/**
 * 区域奖项管理
 */
class RegionReward extends AdminController
{

    //*------------------------------------------------------ */
    //-- 初始化
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new RegionRewardModel();
    }
    /*------------------------------------------------------ */
    //--首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $usersRole = (new RegionRewardModel)->getRows();//分销身份

        $data = $this->getPageList($this->Model);
        foreach ($data['list'] as $key => $row) {
            $row['limit_role'] = explode(',', $row['limit_role']);
            $limit_role_name = [];
            foreach ($row['limit_role'] as $role_id) {
                if ($role_id == 0) {
                    $limit_role_name[] = '粉丝';
                } else {
                    $limit_role_name[] = $usersRole[$role_id]['role_name'];
                }
            }
            $row['limit_role_name'] = join(',', $limit_role_name);
            $data['list'][$key] = $row;
        }

        $this->assign("data", $data);
        if ($runData == false) {
            $data['content'] = $this->fetch('list');
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 详细页调用
    /*------------------------------------------------------ */
    public function asInfo($data)
    {
        $this->assign("UsersRole", (new RegionRewardModel)->getRows());//分销身份
        $this->assign('d_level', config('config.dividend_level'));

        $this->assign('roleGoods', (new RegionRewardModel)->getRows());
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加前处理
    /*------------------------------------------------------ */
    public function beforeAdd($data)
    {
        $count = $this->Model->where('reward_name', $data['reward_name'])->count('id');
        if ($count > 0) return $this->error('已存在相同的奖项名称，不允许重复添加.');
        if (empty($data['reward_name']) == true) {
            return $this->error('请填写分销奖项名称.');
        }
        if (empty($data['reward_type']) == true) {
            return $this->error('请选择分销奖项等级.');
        }

        if (empty($data['region_reward']) == true) {
            return $this->error('请填写区域代理奖比例.');
        }

        if (empty($data['reward_type']) == true) {
            return $this->error('请填写级差奖比例.');
        }

        $data['add_time'] = time();
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加后处理
    /*------------------------------------------------------ */
    public function afterAdd($data)
    {
//        dump($data);exit;
        $this->_Log($data['id'], '添加分销奖项:' . $data['reward_name']);
    }
    /*------------------------------------------------------ */
    //-- 修改前处理
    /*------------------------------------------------------ */
    public function beforeEdit($data)
    {
        $info = $this->Model->find($data['id'])->toArray();
        $this->checkUpData($info, $data);
        $where[] = ' id <> ' . $data['id'];
        $where[] = " reward_name = '" . $data['reward_name'] . "' ";
        $count = $this->Model->where(join(' AND ', $where))->count('id');
        if ($count > 0) return $this->error('已存在相同的奖项名称，不允许重复添加.');
        if (empty($data['reward_name']) == true) {
            return $this->error('请填写分销奖项名称.');
        }
        if (empty($data['reward_type']) == true) {
            return $this->error('请选择分销奖项等级.');
        }

        if (empty($data['region_reward']) == true) {
            return $this->error('请填写区域代理奖比例.');
        }

        if (empty($data['reward_type']) == true) {
            return $this->error('请填写级差奖比例.');
        }

        $data['update_time'] = time();
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 修改后处理
    /*------------------------------------------------------ */
    public function afterEdit($data)
    {
        $this->_Log($data['id'], '修改分销奖项:' . $data['reward_name']);
    }
    /*------------------------------------------------------ */
    //-- 删除
    /*------------------------------------------------------ */
    public function delete()
    {
        $id = input('id/d');
        if ($id < 1) {
            return $this->error('传参错误.');
        }
        $data = $this->Model->where('id', $id)->find();
        if (empty($data) == true) {
            return $this->error('没有找到相关奖项.');
        }
        $res = $this->Model->where('id', $id)->delete();
        if ($res < 1) return $this->error('删除失败，请重试.');
        $this->Model->cleanMemcache();
        $this->_Log($data['id'], '删除分销奖项:' . $data['reward_name']);
        return $this->success('删除成功.');
    }
}
